package xm.components.checkbox
{
	import flash.display.DisplayObjectContainer;
	import flash.events.EventDispatcher;
	import flash.events.IEventDispatcher;
	import flash.events.MouseEvent;
	
	import xm.align.AlignFactory;
	import xm.definition.Arrange;
	import xm.events.SelectionEvent;
	import xm.interfaces.ILifeCycle;
	
	
	
	
	/**
	 * 체크박스 선택 이벤트. 
	 */	
	[Event(name="selectionChange", type="xm.events.SelectionEvent")]
	
	/**
	 * <br> 클래스 설명 : </br>
	 * @author 김정환
	 */	
	public class CheckGroup extends EventDispatcher implements ILifeCycle, IEventDispatcher
	{
		
		//-----------------------------------------------------------------------------------
		// public vars
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * 정렬의 시작점 x 
		 */		
		public var x : Number = 0;
		
		/**
		 * 정렬의 시작점 y 
		 */		
		public var y : Number = 0;
		
		/**
		 * 라디오 버튼들 사이의 갭 ( default = 5 );
		 */		
		public var gapBetweenCheckBoxs : Number = 5;
		
		/**
		 * arrange 가 "" 일 경우 자동정렬하지 않음.
		 * Arrange = default - Arrange.HORIZONTAL 
		 */		
		public var arrange : String = "horizontal";
		//-----------------------------------------------------------------------------------
		// protected / internal vars
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private vars
		//-----------------------------------------------------------------------------------
		
		private var _list : Array = [];
		private var _selectedIndices : Array = [];
		private var _selectedBtns : Array = [];
		private var _af : AlignFactory = new AlignFactory();
		
		
		//-----------------------------------------------------------------------------------
		// public functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * constructor 
		 */	
		public function CheckGroup()
		{
			
		}
		
		

		/**
		 * @param btn
		 */		
		public function addCheckBox( btn : CheckBox ):void
		{
			_list.push( btn );
			btn.addEventListener( MouseEvent.CLICK, onBtnClick );
		}
		
		
		/**
		 * @param list
		 */		
		public function addCheckBoxList( list : Array ):void
		{
			var i:int, len:int;
			
			i=0;
			len=list.length;
			while( i<len )
			{
				( list[ i ] as CheckBox ).addEventListener( MouseEvent.CLICK, onBtnClick );
				++i;
			}
			
			_list.concat( list );
		}
		
		/**
		 * @param btn
		 * @param index
		 */		
		public function addCheckBoxAt( btn : CheckBox, index : int ):void
		{
			btn.addEventListener( MouseEvent.CLICK, onBtnClick );
			_list.splice( index, 0 , btn );
		}
		
		/**
		 * @param btn
		 */		
		public function removeCheckBox( btn : CheckBox ):void
		{
			var idx : int = _list.indexOf( btn );
			_list.splice( idx, 1 );
			
			btn.removeEventListener( MouseEvent.CLICK, onBtnClick );
		}
		
		/**
		 * @param index
		 */		
		public function removeCheckBoxAt( index : int ):void
		{
			( _list[ index ] as CheckBox ).removeEventListener( MouseEvent.CLICK, onBtnClick );
			_list.splice( index, 1 );
			
		}
		
		
		/**
		 * <br>arrange == "" 일경우 수동정렬</br>
		 * <br>라디오 버튼들 정렬.</br>
		 * @param parent - 부모.
		 */		
		public function updateDisplayList( parent : DisplayObjectContainer ):void
		{
			if( arrange == "" )
			{
				
			}
			else
			{
				var i:int = 0, len : int = _list.length;
				
				
				_af.destroy();
				_af.setAlignFactory(0,null, arrange);
				_af.setPosition( x, y );
				
				while( i<len )
				{
					if( i > 0 )
					{
						if( arrange == Arrange.HORIZONTAL ){
							_af.addSimpleItem( _list[ i ], [0,0,0,gapBetweenCheckBoxs] );
						}else{
							_af.addSimpleItem( _list[ i ], [gapBetweenCheckBoxs,0,0,0] );
						}
					}
					else
					{
						_af.addSimpleItem( _list[ i ] );
					}
					++i;
				}
				
				_af.workingAlign();
			}
		}
		
		
		//-----------------------------------------------------------------------------------
		// life cycle functions
		//-----------------------------------------------------------------------------------
		
		/**
		 * createChildren
		 */		
		public function createChildren():void
		{
			
		}
		
		/**
		 * createComplete
		 */		
		public function creationComplete():void
		{
			
		}
		
		/**
		 * add
		 */		
		public function add():void
		{
			
		}
		
		/**
		 * remove
		 */		
		public function remove():void
		{
			
		}
		
		
		/**
		 * destroy
		 */		
		public function destroy():void
		{
			var i:int , len : int, check : CheckBox;
			
			while( i<len )
			{
				check = _list[ i ] as CheckBox;
				check.removeEventListener(MouseEvent.CLICK, onBtnClick );
				check.destroy();
				
				++i;
			}
			_list = null;
			
			x = NaN;
			y = NaN;
			gapBetweenCheckBoxs = NaN;
			arrange = null;
			
			i=0;
			len=_selectedIndices.length;
			while( i<len )
			{
				delete _selectedIndices[ i ];
				++i;
			}
			_selectedIndices = null;
			
			i=0;
			len=_selectedBtns.length;
			
			while( i<len )
			{
				delete _selectedBtns[ i ];
				++i;
			}
			
			
			_af.destroy();
			_af = null;
			
		}
		
		//-----------------------------------------------------------------------------------
		// setter / getter
		//-----------------------------------------------------------------------------------
		
		/**
		 * @return 
		 */		
		public function get selectedBtns():Array
		{
			return _selectedBtns;
		}
		
		
		/**
		 * <br>버튼으로 아이템 선택시키기.</br>
		 * @param value
		 */		
		public function set selectedBtns(list:Array):void
		{
			var i:int, len : int, indices : Array;
			if( _selectedBtns && _selectedBtns.length > 0 )
			{
				i=0;
				len = _selectedBtns.length;
				
				while( i<len )
				{
					_selectedBtns[ i ].selected = false;
					++i;
				}
			}
			
			_selectedBtns = list;
			
			i=0;
			len = _selectedBtns.length;
			indices = [];
			
			while( i<len )
			{
				_selectedBtns[ i ].selected = true;
				indices.push( _list.indexOf( _selectedBtns[ i ] ) );
				++i;
			}
			
			_selectedIndices = indices;
		}
		
		
		/**
		 * <br>선택된 아이템의 인덱스 넘버 반환</br>
		 * @return 
		 */		
		public function get selectedIndices():Array
		{
			return _selectedIndices.sort( Array.CASEINSENSITIVE ) as Array;
		}
		
		/**
		 * <br>인덱스 번호로 버튼 선택시키기.</br>
		 * null = 선택해제 하기.
		 * <br />
		 * <br />
		 * @param value
		 */		
		public function set selectedIndices(indices:Array):void
		{
			var i:int, len : int, btns : Array, btn : CheckBox;
			if( _selectedBtns && _selectedBtns.length > 0 )
			{
				i=0;
				len = _selectedBtns.length;
				
				while( i<len )
				{
					_selectedBtns[ i ].selected = false;
					++i;
				}
			}
			
			_selectedIndices = indices;
			if( _selectedIndices == null )
			{
				_selectedBtns = null;
			}
			else
			{
				i=0;
				len = _selectedIndices.length;
				btns = [];
				
				while( i<len )
				{
					btn = _list[ _selectedIndices[i] ];
					btn.selected = true;
					btns.push( btn );
					++i;
				}
				
				_selectedBtns = btns;
			}
			
		}
		
		//-----------------------------------------------------------------------------------
		// protected functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// internal functions
		//-----------------------------------------------------------------------------------
		
		
		
		
		//-----------------------------------------------------------------------------------
		// private functions
		//-----------------------------------------------------------------------------------
		
		
		
		//-----------------------------------------------------------------------------------
		// event handler
		//-----------------------------------------------------------------------------------
		
		
		/**
		 * @param e
		 */		
		protected function onBtnClick(e:MouseEvent):void
		{
			var currentBtn : CheckBox = ( e.currentTarget as CheckBox ),
				idx : int = _list.indexOf( currentBtn );
			
			if( _selectedBtns == null ) _selectedBtns = [];
			if( _selectedIndices == null ) _selectedIndices = [];
			
			if( currentBtn.selected )
			{
				_selectedBtns.push( currentBtn );
				_selectedIndices.push( idx );
			}
			else
			{
				_selectedBtns.splice( _selectedBtns.indexOf( currentBtn ) , 1 );
				_selectedIndices.splice( _selectedIndices.indexOf( idx ) , 1 );
			}
			
			var checkEvent : SelectionEvent = new SelectionEvent( SelectionEvent.SELECTION_CHANGE, false, false );
			checkEvent.data = selectedIndices;
			
			dispatchEvent( checkEvent );
		}
		
		
		
		
		
		
		
	}
}